﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

using XmlStudio.Logger;
using XmlStudio.DataLayer.Interfaces;

namespace XmlStudio.DataLayer
{
    public class BigXmlReader : IBigXmlReader
    {
        #region Fields
        private string path;
        private bool opened = false;
        private XmlReader reader;
        #endregion

        #region Constructors
        public BigXmlReader(string path)
        {
            this.path = path;

            try
            {
                this.reader = XmlReader.Create(path);
                this.opened = true;
            }
            catch (System.ArgumentNullException ex)
            {
                Log.CurrentLogWriter.Write(ex, 1, "BigXmlReader");
            }
            catch (System.IO.IOException ex)
            {
                Log.CurrentLogWriter.Write(ex, 1, "BigXmlReader");
            }
            catch (System.Security.SecurityException ex)
            {
                Log.CurrentLogWriter.Write(ex, 1, "BigXmlReader");
            }
            catch (System.UriFormatException ex)
            {
                Log.CurrentLogWriter.Write(ex, 1, "BigXmlReader");
            }
        }
        #endregion

        #region Properties
        public bool Opened
        {
            get { return this.opened; }
        }
        #endregion

        #region Methods
        public void Close()
        {
            this.reader.Close();
            this.opened = false;
            this.reader = null;
        }
        #endregion

        #region Static methods
        public static IBigXmlReader OpenXml(string path)
        {
            BigXmlReader xmlReader = new BigXmlReader(path);
            return xmlReader;
        }
        #endregion
    }
}